//
//  HXSetInterfaceViewController.m
//  HongxuanBluetooth
//
//  Created by 广州弘轩科技发展有限公司 on 2023/8/16.
//

#import "HXSetInterfaceViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import "STPhotoKitController.h"
#import "UIImagePickerController+ST.h"
#import "STConfig.h"
typedef NS_ENUM(NSInteger, PhotoType)
{
    PhotoTypeIcon,
    PhotoTypeRectangle,
    PhotoTypeRectangle1
};

@interface HXSetInterfaceViewController ()
@property (nonatomic ,strong)UIImageView *mainscreen;
@property (nonatomic ,strong)UIImageView *statebj;
@property (nonatomic ,strong)UIImageView *languageview;
@property(nonatomic,strong) UITextView *textView;
@property (nonatomic ,strong)UIImageView *checkbox;

@property (nonatomic ,strong)UIImageView *headview;
@property (nonatomic ,strong)UIButton *headviewbt;

@property (nonatomic, assign) PhotoType type;
@property (strong, nonatomic) BlueToothManager *blueToothManager;//蓝牙

@end

@implementation HXSetInterfaceViewController
- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    // Reload your data here, and this gets called
    // before the view transition is complete.
    NSLog(@"viewWillAppear");
}

- (void)viewDidAppear:(BOOL)animated {

    [super viewDidAppear:animated];
    // Reload your data here, and this gets called
    // after the view transition is complete.
    NSLog(@"viewDidAppear");
}
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"viewDidLoad");
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    self.fd_prefersNavigationBarHidden = YES;
    self.blueToothManager = [BlueToothManager shareInstance];
    self.blueToothManager.autoConnect = YES;
    [self.blueToothManager StartScan];
    
    [self BottomFunction];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changelanguages) name:@"changelanguages" object:nil];
}
-(void)changelanguages{
//    [self.checkbox removeFromSuperview];
//    [self addSelectCorrespondence];
//    if([themeColors isEqualToString:@"default"]){
////            mainscreen.image = [UIImage imageNamed:@"mainscreen"];
//        self.checkbox.image = [UIImage imageNamed:@"checkbox"];
//    }else{
//        self.checkbox.layer.cornerRadius = 8;
//        self.checkbox.layer.masksToBounds = YES;
//        self.checkbox.backgroundColor = midnightRGB;
//    }
    UIImageView *view = (UIImageView*)[self.view viewWithTag:10];
    UIImageView *viewtow = (UIImageView*)[self.view viewWithTag:11];
    UIImageView *viewthree = (UIImageView*)[self.view viewWithTag:12];
    UIImageView *viewfour = (UIImageView*)[self.view viewWithTag:13];
    if([themeColors isEqualToString:@"default"]){
        self.mainscreen.image = [UIImage imageNamed:@"optionmian"];
        self.languageview.image = [UIImage imageNamed:@"checkbox"];
        self.languageview.backgroundColor = [UIColor blackColor];
        
        self.checkbox.image = [UIImage imageNamed:@"checkbox"];
        self.checkbox.backgroundColor = [UIColor blackColor];
        
        self.statebj.backgroundColor = [UIColor blackColor];
        
        
        view.image = [UIImage imageNamed:@"checkbox"];
        view.backgroundColor = [UIColor blackColor];
        
        viewtow.image = [UIImage imageNamed:@"checkbox"];
        viewtow.backgroundColor = [UIColor blackColor];
        
        viewthree.image = [UIImage imageNamed:@"checkbox"];
        viewthree.backgroundColor = [UIColor blackColor];
        
        viewfour.image = [UIImage imageNamed:@"checkbox"];
        viewfour.backgroundColor = [UIColor blackColor];
    }else{
        self.mainscreen.image = [UIImage imageNamed:@"optionmidnight"];
        self.languageview.backgroundColor = midnightRGB;
        self.languageview.image = [UIImage imageNamed:@""];
        
        self.checkbox.backgroundColor = midnightRGB;
        self.checkbox.image = [UIImage imageNamed:@""];
        
        self.statebj.backgroundColor = midnightRGB;
        
        view.image = [UIImage imageNamed:@""];
        view.backgroundColor = midnightRGB;
        
        viewtow.image = [UIImage imageNamed:@""];
        viewtow.backgroundColor = midnightRGB;
        
        viewthree.image = [UIImage imageNamed:@""];
        viewthree.backgroundColor = midnightRGB;
        
        viewfour.image = [UIImage imageNamed:@""];
        viewfour.backgroundColor = midnightRGB;
    }
    
    
    
    
    
    
    

    if([themeColors isEqualToString:@"default"]){
//            mainscreen.image = [UIImage imageNamed:@"mainscreen"];
        self.checkbox.image = [UIImage imageNamed:@"checkbox"];
    }else{

        self.checkbox.backgroundColor = midnightRGB;
    }
}
-(void)BottomFunction{
    
    UIImageView *tabr = [[UIImageView alloc] initWithFrame:CGRectMake(0, kNavHeight-44, winWidth, 44)];
    [self.view addSubview:tabr];
    UIImageView *mainscreen = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, winWidth, 44)];
    if([themeColors isEqualToString:@"default"]){
        mainscreen.image = [UIImage imageNamed:@"optionmian"];
        
    }else{
        mainscreen.image = [UIImage imageNamed:@"optionmidnight"];
    }
    self.mainscreen = mainscreen;
    [tabr addSubview:mainscreen];
    
    NSInteger width = 0;
    NSInteger mastop = 0;
    if(winHeight<=736){
        width = 120;
        mastop = 5;
    }else{
        width = 140;
        mastop = 20;
    }
    
//    UIImageView *headview = [[UIImageView alloc] init];
////    headview.backgroundColor = [UIColor whiteColor];
//    headview.image = [UIImage imageNamed:@"headview"];
//    headview.layer.cornerRadius = width/2;
//    headview.layer.masksToBounds = YES;
//    self.headview = headview;
//    [self.view addSubview:headview];
//    [headview mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(tabr.mas_bottom).offset(30);
//        make.centerX.mas_equalTo(self.view);
//        make.width.height.mas_equalTo(width);
//    }];
    
    
    
    self.headviewbt = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.headviewbt addTarget:self action:@selector(headbt) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.headviewbt];
    
    
    //加载首先访问本地沙盒是否存在相关图片
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"currentImage.png"];
    UIImage *savedImage = [UIImage imageWithContentsOfFile:fullPath];
    
    if (!savedImage)
    {
        //默认头像
        [self.headviewbt setImage:[UIImage imageNamed:@"headview"] forState:UIControlStateNormal];
    }
    else
    {
        [self.headviewbt setImage:savedImage forState:UIControlStateNormal];
    }
    
    self.headviewbt.layer.masksToBounds = YES;
    
    self.headviewbt.layer.cornerRadius = width / 2;
    
    
    [self.headviewbt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(tabr.mas_bottom).offset(30);
        make.centerX.mas_equalTo(self.view);
        make.width.height.mas_equalTo(width);
    }];
    
//    magic
    
//    UIImageView *magicview = [[UIImageView alloc] init];
//    magicview.image = [UIImage imageNamed:@"magic"];
//    [self.view addSubview:magicview];
//    [magicview mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(headview.mas_bottom).offset(13);
//        make.centerX.mas_equalTo(self.view);
//        make.width.mas_equalTo(177);
//        make.height.mas_equalTo(53);
//    }];
    
    self.textView = [[UITextView alloc]init];
//    self.textView.text = textViewname;
    NSString *textViewname = [[NSUserDefaults standardUserDefaults] objectForKey:@"textViewname"];
    if(textViewname.length>0){
        self.textView.text = textViewname;
    }else{
        self.textView.text = @"BLUETHER MAGIC ";
    }
    [self.view addSubview:self.textView];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headviewbt.mas_bottom).offset(13);
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(177);
        make.height.mas_equalTo(33);
    }];
    self.textView.delegate = self;
    
    //修复文本框是偏移（下移）
//    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.textView.backgroundColor = [UIColor blackColor];
    //设置文本
//    self.textView.text = @"我是UITextView，大家欢迎使用。";
    //设置文字对齐方式属性
    self.textView.textAlignment = NSTextAlignmentCenter;
    //设置文字对齐方
    //设置文字颜色属性
    self.textView.textColor = [UIColor whiteColor];
    //设置文字字体属性
    self.textView.font = [UIFont systemFontOfSize:16.0f];
    //设置编辑使能属性,是否允许编辑（=NO时，只用来显示，依然可以使用选择和拷贝功能）
    self.textView.editable = YES;
    
    self.textView.keyboardType = UIKeyboardTypeEmailAddress;
    
    
    UIImageView *btvie = [[UIImageView alloc] init];
    btvie.image = [UIImage imageNamed:@"attributeentry"];
    [self.view addSubview:btvie];
    [btvie mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.textView.mas_bottom).offset(5);
        make.width.height.mas_equalTo(25);
        make.centerX.mas_equalTo(self.view);
    }];
    
    UIButton *btviebt = [UIButton buttonWithType:UIButtonTypeCustom];
    [btviebt addTarget:self action:@selector(editStatusbt) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btviebt];
    [btviebt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.textView.mas_bottom).offset(5);
        make.width.height.mas_equalTo(25);
        make.centerX.mas_equalTo(self.view);
    }];
    
//    language
    self.languageview =[[UIImageView alloc] init];
    if([themeColors isEqualToString:@"default"]){
//            mainscreen.image = [UIImage imageNamed:@"mainscreen"];
        self.languageview.image = [UIImage imageNamed:@"checkbox"];
    }else{
        self.languageview.layer.cornerRadius = 8;
        self.languageview.layer.masksToBounds = YES;
        self.languageview.backgroundColor = midnightRGB;
    }
    
    self.languageview.userInteractionEnabled = YES;
    [self.view addSubview:self.languageview];
    [self.languageview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(btvie.mas_bottom).offset(mastop);
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(315);
        make.height.mas_equalTo(47);
    }];
    
    UIImageView *language =[[UIImageView alloc] init];
    language.image = [UIImage imageNamed:@"language"];
    language.userInteractionEnabled = YES;
    [self.languageview addSubview:language];
    [language mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(13);
        make.left.mas_equalTo(17);
        make.width.mas_equalTo(24);
        make.height.mas_equalTo(20);
    }];
//    languagename
    UIImageView *languagename =[[UIImageView alloc] init];
    languagename.image = [UIImage imageNamed:@"languagename"];
    [self.languageview addSubview:languagename];
    [languagename mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(13);
        make.left.mas_equalTo(language.mas_right).offset(11.5);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(21);
        
    }];
    
    UIButton *languagebt = [UIButton buttonWithType:UIButtonTypeCustom];
    languagebt.tag = 5;
    [languagebt addTarget:self action:@selector(selectCorrespondence:) forControlEvents:UIControlEventTouchUpInside];
    [self.languageview addSubview:languagebt];
    [languagebt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(self.languageview);
    }];
    
    
    UILabel *statename = [[UILabel alloc] init];
    statename.text = @"ENG";
    statename.textAlignment = NSTextAlignmentLeft;
    statename.textColor = [UIColor whiteColor];
    statename.font = [UIFont systemFontOfSize:25];
    [self.languageview addSubview:statename];
    [statename mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-16);
        make.top.mas_equalTo(11);
        make.height.mas_equalTo(25);
    }];
    
    UIImageView *nationalflagview = [[UIImageView alloc] init];
    nationalflagview.image = [UIImage imageNamed:@"English"];
    [self.languageview addSubview:nationalflagview];
    [nationalflagview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(13);
        make.width.height.mas_equalTo(20.33);
        make.right.mas_equalTo(statename.mas_left).offset(-8);
    }];
    
    [self addSelectCorrespondence];
    
    UIImageView *statebj = [[UIImageView alloc] init];
    if([themeColors isEqualToString:@"default"]){
        statebj.backgroundColor = [UIColor blackColor];
    }else{
        statebj.backgroundColor = midnightRGB;
    }
    self.statebj = statebj;
    [self.view addSubview:statebj];
    [statebj mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.mas_equalTo(0);
        make.bottom.mas_equalTo(-SafeAreaBottomHeight-34);
        make.height.mas_equalTo(50);
    }];
#pragma mark 返回入口
    UIImageView *backview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"back"]];
    backview.userInteractionEnabled = YES;
    [self.view addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-SafeAreaBottomHeight-37);
        make.width.mas_equalTo(33);
        make.height.mas_equalTo(36);
        make.left.mas_equalTo(26);
    }];
    
    UIButton *backbt = [UIButton buttonWithType:UIButtonTypeCustom];
    [backbt addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [backview addSubview:backbt];
    [backbt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(backview);
    }];
    
    
    
    UIImageView *bluetherview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bluether"]];
    bluetherview.userInteractionEnabled = YES;
    [self.view addSubview:bluetherview];
    [bluetherview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.bottom.mas_equalTo(-SafeAreaBottomHeight-52);
        make.height.mas_equalTo(13);
    }];

}

-(void)addSelectCorrespondence{
    NSInteger width = 0;
    NSInteger mastop = 0;
    if(winHeight<=736){
        width = 155-5;
        mastop = 15;
    }else{
        width = 175;
        mastop = 26;
    }
//    subordinate箭头
    NSArray *viewarray = @[@"heipsupport",@"feefback",@"appinform",@"btrepair",@"styleswitching"];
    NSArray *namearray = @[@"heipsupportname",@"feefbackname",@"appinformname",@"btrepairname",@"styleswitchingname"];
    
    for (NSInteger i = 0; i < viewarray.count; i ++) {
        
        self.checkbox =[[UIImageView alloc] init];
        if([themeColors isEqualToString:@"default"]){
//            mainscreen.image = [UIImage imageNamed:@"mainscreen"];
            self.checkbox.image = [UIImage imageNamed:@"checkbox"];
        }else{
            self.checkbox.layer.cornerRadius = 8;
            self.checkbox.layer.masksToBounds = YES;
            self.checkbox.backgroundColor = midnightRGB;
        }
        
        self.checkbox.userInteractionEnabled = YES;
        self.checkbox.tag =10+i;
        self.checkbox.frame = CGRectMake((winWidth-315)/2, 11.6+kNavHeight+width+40+mastop+73+i*(11.6+47), 315, 47);
        [self.view addSubview:self.checkbox];
        
        UIButton *checkboxbt = [UIButton buttonWithType:UIButtonTypeCustom];
        checkboxbt.frame = CGRectMake(0, 0, 315, 47);
        checkboxbt.tag = i;
        [checkboxbt addTarget:self action:@selector(selectCorrespondence:) forControlEvents:UIControlEventTouchUpInside];
        [self.checkbox addSubview:checkboxbt];
        
        UIImageView *subordinate = [[UIImageView alloc] init];
        subordinate.frame = CGRectMake(315-34, 12.6, 18.67, 21);
        subordinate.image = [UIImage imageNamed:@"subordinate"];
        [self.checkbox addSubview:subordinate];
        
        UIImageView *arrayview = [[UIImageView alloc] init];
        arrayview.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",viewarray[i]]];
        
        UIImageView *name = [[UIImageView alloc] init];
        name.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",namearray[i]]];
        [self.checkbox addSubview:name];
        [self.checkbox addSubview:arrayview];
        switch (i) {
            case 0:
                arrayview.frame = CGRectMake(19, 13, 20.67, 20.67);
                name.frame = CGRectMake(19+20.67+13.3, 14.3, 176, 21.33);
                break;
            case 1:
                arrayview.frame = CGRectMake(19, 13, 20.67, 20.67);
                name.frame = CGRectMake(19+20.67+13.3, 14.3, 100, 17.33);
                break;
            case 2:
                arrayview.frame = CGRectMake(19, 13, 20.67, 20.67);
                name.frame = CGRectMake(19+20.67+13.3, 14.3, 127, 21.33);
                break;
            case 3:
                arrayview.frame = CGRectMake(22.3, 13, 13.67, 20.33);
                name.frame = CGRectMake(22.3+20.33+17, 14.3, 201, 21);
                break;
            case 4:
                arrayview.frame = CGRectMake(19, 13, 21.67, 20.33);
                name.frame = CGRectMake(19+20.33+12.67, 14.3, 190, 21.33);
                break;
                
            default:
                break;
                
        }
        
    }
    
}
- (void)headbt{
    
    NSLog(@"点击了头像");
//    self.type = PhotoTypeRectangle1;
//    [self editImageSelected];
    [self changeIcon];
}
-(void)back{
    
//    [self dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)selectCorrespondence:(UIButton *)button{
    
    NSLog(@"选择了对应功能%ld",(long)button.tag);
    switch (button.tag) {
        case 0:
//            help support
            [self jumpHelpSupport];
            break;
        case 1:
//            feedback
            [self jumpFeedBack];
            break;
        case 2:

            [self jumpAppInform];
            break;
        case 3:
            NSLog(@"点击了3");
            [self bluetoothrepair];
            break;
        case 4:
            NSLog(@"点击了4");
            [self jumpstyleswitchingmian];
            break;
        case 5:
            NSLog(@"点击了5");
            if(!XHbluetoolUtils.beginurl){
                
            }else{
                [self jumplaguage];
                
            }
            break;
            
        default:
            break;
    }
}
- (void)editStatusbt{
    
    NSLog(@"打开编辑状态");
    [self.textView becomeFirstResponder];

}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    NSLog(@"将要开始编辑？");
    return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
    NSLog(@"将要结束编辑？");
    
    return YES;
}

- (void)textViewDidBeginEditing:(UITextView *)textView{
    NSLog(@"开始编辑。");
//    self.placeholderLabel.text = @"";
}
- (void)textViewDidEndEditing:(UITextView *)textView{
    NSLog(@"结束编辑。");
    NSLog(@"%@",textView.text);
    
    if(textView.text.length >15){
        [SVProgressHUD showSuccessWithStatus:@"Maximum 15 lengths"];
        return;
    }
    
    //    存入蓝牙时间
    [[NSUserDefaults standardUserDefaults] setObject:textView.text forKey:@"textViewname"];
    //这里建议同步存储到磁盘中，但是不是必须的
    [[NSUserDefaults standardUserDefaults] synchronize];
    
//    NSString *strdata = [NSString stringWithFormat:@":%@",textView.text];
//
//    NSData *testData = [strdata dataUsingEncoding: NSUTF8StringEncoding];//字符串转化成 data
//    Byte*testByte = (Byte*)[testData bytes];
//    uint8_t*bytes =malloc(sizeof(*bytes)*testData.length);
//    for(int i=0;i < [testData length];i++){
//        NSLog(@"myByte = %d\n",testByte[i]);
//        bytes[i] = testByte[i];
//
//    }
//    NSData*adata = [[NSData alloc]initWithBytes:bytes length:testData.length];
//    NSLog(@"adata :%@",adata);
//
//    [self.blueToothManager writeData:adata];
    
    

}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    NSLog(@"将要改变内容？");
    if([text isEqualToString:@"\n"]){
        [textView resignFirstResponder];
        return NO;
        
    }
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView{
    NSLog(@"改变内容。");
}

- (void)textViewDidChangeSelection:(UITextView *)textView{
    NSLog(@"选中内容。");
}
//#pragma mark- 按钮点击事件实现方法
//- (void)btnClick:(UIButton*)sender{
//    NSLog(@"添加内容：你好，我好，大家好！");
//
//    self.textView.text = [self.textView.text stringByAppendingString:@"你好，我好，大家好！\n"];
//
//    NSRange range = NSMakeRange([self.textView.text length]- 1, 1);
//    [self.textView scrollRangeToVisible:range];
//
//    [self.contentView endEditing:YES];
//}

/*
 **监听点击事件，当点击非textfiled位置的时候，所有输入法全部收缩
 */
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [self.view endEditing:YES];
}
- (void)selectsendtous{
    NSLog(@"点击了selectsendtous");
    self.textView.text = @"";
}

- (void)bluetoothrepair{
    
    WKWebViewController *web = [[WKWebViewController alloc] init];
    [web loadWebURLSring:@"https://www.bluethermagic.com/pages/repair-services-1?isPreview=1"];
    [self.navigationController pushViewController:web animated:YES];
    
}
-(void)jumpHelpSupport{
    
//    HXHelpSupportViewController
    HXHelpSupportViewController*jum = [[HXHelpSupportViewController alloc] init];
    jum.modalPresentationStyle = NSDateFormatterNoStyle;
//    [self presentViewController:jum animated:YES completion:nil];
    [self.navigationController pushViewController:jum animated:YES];
}
-(void)jumpFeedBack{
    
//    HXFeedBackViewController
    HXFeedBackViewController*jum = [[HXFeedBackViewController alloc] init];
    jum.modalPresentationStyle = NSDateFormatterNoStyle;
//    [self presentViewController:jum animated:YES completion:nil];
    [self.navigationController pushViewController:jum animated:YES];
}
-(void)jumpAppInform{
    
//    HXAppInformViewController
    HXAppInformViewController*jum = [[HXAppInformViewController alloc] init];
    jum.modalPresentationStyle = NSDateFormatterNoStyle;
//    [self presentViewController:jum animated:YES completion:nil];
    [self.navigationController pushViewController:jum animated:YES];
}
//HXLanguageViewController
-(void)jumplaguage{
//    HXLanguageViewController
    HXLanguageViewController*jum = [[HXLanguageViewController alloc] init];
    jum.modalPresentationStyle = NSDateFormatterNoStyle;
//    [self presentViewController:jum animated:YES completion:nil];
    [self.navigationController pushViewController:jum animated:YES];
}
//styleswitchingmian
- (void)jumpstyleswitchingmian{
    //styleswitchingmian
    HXStyleSwitChingViewController*jum = [[HXStyleSwitChingViewController alloc] init];
    jum.returnValueBlock = ^{
//        [self BottomFunction];
    };
    jum.modalPresentationStyle = NSDateFormatterNoStyle;
//    [self presentViewController:jum animated:YES completion:nil];
    [self.navigationController pushViewController:jum animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];

}
//#pragma mark - --- delegate 视图委托 ---
//
//#pragma mark - 1.STPhotoKitDelegate的委托
//
//- (void)photoKitController:(STPhotoKitController *)photoKitController resultImage:(UIImage *)resultImage
//{
//    switch (self.type) {
//        case PhotoTypeIcon:
//            self.headview.image = resultImage;
//            NSLog(@"resultImage:%@",resultImage);
//            break;
//        case PhotoTypeRectangle:
//            self.headview.image = resultImage;
//            break;
//        case PhotoTypeRectangle1:
//            self.headview.image = resultImage;
//            break;
//        default:
//            break;
//    }
//    NSLog(@"resultImage:%@",resultImage);
//}
//
//#pragma mark - 2.UIImagePickerController的委托
//
//- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
//{
//    [picker dismissViewControllerAnimated:YES completion:^{
//        UIImage *imageOriginal = [info objectForKey:UIImagePickerControllerOriginalImage];
//        STPhotoKitController *photoVC = [STPhotoKitController new];
//        [photoVC setDelegate:self];
//        [photoVC setImageOriginal:imageOriginal];
//        switch (self.type) {
//            case PhotoTypeIcon:
//                [photoVC setSizeClip:CGSizeMake(self.headview.width*2, self.headview.height*2)];
//                break;
//            case PhotoTypeRectangle:
//                [photoVC setSizeClip:CGSizeMake(self.headview.width*2, self.headview.height*2)];
//                break;
//            case PhotoTypeRectangle1:
//                [photoVC setSizeClip:CGSizeMake(self.headview.width*2, self.headview.height*2)];
//                break;
//            default:
//                break;
//        }
//
//        photoVC.modalPresentationStyle = NSDateFormatterNoStyle;
//        [self presentViewController:photoVC animated:YES completion:nil];
//    }];
//}
//
//- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
//    [picker dismissViewControllerAnimated:YES completion:^(){
//    }];
//}
//
//#pragma mark - --- event response 事件相应 ---
//- (void)editImageSelected
//{
//    UIAlertController *alertController = [[UIAlertController alloc]init];
//
////    UIAlertAction *action0 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
////        UIImagePickerController *controller = [UIImagePickerController imagePickerControllerWithSourceType:UIImagePickerControllerSourceTypeCamera];
////
////        if ([controller isAvailableCamera] && [controller isSupportTakingPhotos]) {
////            [controller setDelegate:self];
////            [self presentViewController:controller animated:YES completion:nil];
////        }else {
////            NSLog(@"%s %@", __FUNCTION__, @"相机权限受限");
////        }
////    }];
//
//    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"从相册获取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        UIImagePickerController *controller = [UIImagePickerController imagePickerControllerWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
//        [controller setDelegate:self];
//        if ([controller isAvailablePhotoLibrary]) {
//            [self presentViewController:controller animated:YES completion:nil];
//        }    }];
//
//    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
//    }];
//
////    [alertController addAction:action0];
//    [alertController addAction:action1];
//    [alertController addAction:action2];
//
//    [self presentViewController:alertController animated:YES completion:nil];
//}


- (void)changeIcon
{
    UIAlertController *alertController;
    
    __block NSUInteger blockSourceType = 0;
    
    // 判断是否支持相机
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        //支持访问相机与相册情况
        alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"从相册中选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            NSLog(@"点击从相册中选取");
            //相册
            blockSourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            
            UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
            
            imagePickerController.delegate = self;
            
            imagePickerController.allowsEditing = YES;
            
            imagePickerController.sourceType = blockSourceType;
            
            [self presentViewController:imagePickerController animated:YES completion:nil];
        }]];
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            NSLog(@"点击拍照");
            //相机
            blockSourceType = UIImagePickerControllerSourceTypeCamera;
            
            UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
            
            imagePickerController.delegate = self;
            
            imagePickerController.allowsEditing = YES;
            
            imagePickerController.sourceType = blockSourceType;
            
            [self presentViewController:imagePickerController animated:YES completion:nil];
        }]];
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
            NSLog(@"点击取消");
            // 取消
            return;
        }]];
        
        [self presentViewController:alertController animated:YES completion:nil];
    }
    else
    {
        //只支持访问相册情况
        alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"从相册中选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            NSLog(@"点击从相册中选取");
            //相册
            blockSourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            
            UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
            
            imagePickerController.delegate = self;
            
            imagePickerController.allowsEditing = YES;
            
            imagePickerController.sourceType = blockSourceType;
            
            [self presentViewController:imagePickerController animated:YES completion:^{
                
            }];
        }]];
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
            NSLog(@"点击取消");
            // 取消
            return;
        }]];
        
        [self presentViewController:alertController animated:YES completion:nil];
    }
}


#pragma mark - 选择图片后,回调选择

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    
    /* 此处info 有六个可选类型
     * UIImagePickerControllerMediaType; // an NSString UTTypeImage)
     * UIImagePickerControllerOriginalImage;  // a UIImage 原始图片
     * UIImagePickerControllerEditedImage;    // a UIImage 裁剪后图片
     * UIImagePickerControllerCropRect;       // an NSValue (CGRect)
     * UIImagePickerControllerMediaURL;       // an NSURL
     * UIImagePickerControllerReferenceURL    // an NSURL that references an asset in the AssetsLibrary framework
     * UIImagePickerControllerMediaMetadata    // an NSDictionary containing metadata from a captured photo
     */
    
    [self.headviewbt setImage:image forState:UIControlStateNormal];
    
    [self saveImage:image withName:@"currentImage.png"];
}


#pragma mark - 保存图片至本地沙盒

- (void)saveImage:(UIImage *)currentImage withName:(NSString *)imageName
{
    NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.8);
    
    // 获取沙盒目录
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
    
    // 将图片写入文件
    [imageData writeToFile:fullPath atomically:NO];
}


@end
